<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Http\Requests\Order\Picking\UpdateRequest;
use App\Http\Searchs\Order\PickingSearch;
use App\Http\Searchs\Order\PickingShowSearch;
use App\Repositories\Picking;
use Illuminate\Support\Facades\DB;

class PickingController extends Controller
{
    /**
     * @var Picking
     * @author luffyzhao@vip.126.com
     */
    private $picking;

    /**
     * PickingController constructor.
     * @param Picking $picking
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Picking $picking)
    {
        $this->picking = $picking;
    }

    /**
     * @param PickingSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(PickingSearch $search)
    {
        return $this->response(
            $this->picking->paginate($search->toArray())
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->picking->update($id, $request->only(['user_id']));
            })
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function picking($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->picking->picking($id);
            })
        );
    }

    /**
     * @param PickingShowSearch $search
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function show(PickingShowSearch $search, $id)
    {
        return $this->response(
            $this->picking->show($search->toArray(), $id)
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function printForOrder($id)
    {
        return $this->response(
            $this->picking->printForOrder($id)
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function printForLocation($id)
    {
        return $this->response(
            $this->picking->printForLocation($id)
        );
    }
}
